'''
Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
Date: 2025-09-06 18:51:02
LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
LastEditTime: 2025-09-28 20:44:55
FilePath: /ml-pro/project/parse_data.py
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
'''
import csv
import sys
import json
import pandas as pd
from typing import List, Dict, Any

def parse_csv_to_standard(input_path, output_path):
    with open(input_path, 'r', encoding='utf-8') as in_f, \
         open(output_path, 'w', encoding='utf-8', newline='') as out_f:
        
        reader = csv.reader(in_f)
        writer = csv.writer(out_f)
        
        # 写入CSV标题头
        writer.writerow(['issue', 'date', 'red1', 'red2', 'red3', 'red4', 'red5', 'red6', 'blue'])
        
        # 跳过标题行（如果有）
        next(reader, None)
        
        for row in reader:
            # 处理红球数字（分割空格并取前6个）
            red_balls = row[2].split()[:6]
            blue_ball = row[3]
            
            # 确保红球是整数（去除前导零）
            red_balls = [str(int(ball)) for ball in red_balls]
            
            writer.writerow([
                row[0],          # 期号
                row[1],          # 日期
                *red_balls,      # 展开6个红球
                blue_ball        # 蓝球
            ])

def parse_predict_data_from_json(json_file_path):
    """
    分析双色球JSON数据并转换为CSV格式
    """
    try:
        with open(json_file_path, 'r', encoding='utf-8') as file:
            data = json.load(file)
        
        # 转换数据格式
        processed_data = []
        
        # 检查数据是单个对象还是数组
        if isinstance(data, list):
            # 如果是数组，处理每个对象
            for item in data:
                processed_item = process_single_item(item)
                processed_data.append(processed_item)
        elif isinstance(data, dict):
            # 如果是单个对象
            processed_item = process_single_item(data)
            processed_data.append(processed_item)
        else:
            raise ValueError("JSON数据格式不正确")
        
        # 创建DataFrame
        df = pd.DataFrame(processed_data)

        # 过滤红球准确率大于0.6的数据 
        df = df[df["red_accuracy"] > 0.6]
        
        # 保存到CSV文件
        df.to_csv('predict_result.csv', index=False, encoding='utf-8-sig')
        print("数据已成功保存到 predict_result.csv")
        
        return df
        
    except FileNotFoundError:
        print(f"错误: 文件 {json_file_path} 未找到")
    except json.JSONDecodeError:
        print("错误: JSON格式不正确")
    except KeyError as e:
        print(f"错误: 缺少必要的键 {e}")
    except Exception as e:
        print(f"处理数据时发生错误: {e}")

def process_single_item(item: Dict[str, Any]) -> Dict[str, Any]:
    """
    处理单个JSON对象，转换为目标格式
    """
    processed = {
        'time': item.get('time', '')
    }
    
    # 处理红球，确保有6个球
    red_balls = item.get('red_balls', [])
    if len(red_balls) >= 6:
        for i in range(6):
            processed[f'red{i+1}'] = red_balls[i]
    else:
        # 如果红球数量不足6个，用0填充
        for i in range(6):
            processed[f'red{i+1}'] = red_balls[i] if i < len(red_balls) else 0
    

    processed['blue'] = item.get('blue_ball', 0) # 重命名为blue
    if item.get('red_accuracy', 0) > 0.6 and item.get('blue_accuracy', 0) == 1:
        processed['red_accuracy']= item.get('red_accuracy', 0)
        processed['blue_accuracy']= item.get('blue_accuracy', 0)
    
    return processed


'''
python parse_json.py all_qualified_predictions.json
python parse_json.py data/ssq_data_20250906_211833.csv result.csv
'''
if __name__ == '__main__':
    print("len is: ",len(sys.argv))
    if len(sys.argv) == 2:
        parse_predict_data_from_json(sys.argv[1])
    if len(sys.argv) == 3:
        parse_csv_to_standard(sys.argv[1], sys.argv[2])